import {timeFormat} from '../../model/utils/common';
import { mlist } from '../maintenance/maintenance';

export default {
    namespace: 'createMaintain',
    state: {
        isEdit: false,
        detail : { },
        list: [],
        carModal : false,
        carList : null,
        price: {},
        location: {
            latitude: 0,
            longitude: 0
        }
    },
    reducers: {
        save(state, { payload }) {
            return Object.assign({}, state, payload);
        }
    },
    effects: {
        * getCarList({ payload }, { put, call, select }){
            // 查询汽车列表
            const carList = yield put.resolve({
                type : '$request/get',
                payload : {
                    url : '/api/getCarList'
                }
            });
            // const carNumber = wx.getStorageSync('carNumber');
            // if(!carNumber && carList.length){
            //     wx.setStorageSync('carNumber', carList[0]);
            // }
            yield put({
                type : 'save',
                payload : {
                    carList
                }
            });
            return carList;
        },
        *init({ payload }, { put, call, select }){
            let detail =  {
                time : timeFormat(Date.now(), 'YYYY-MM-DD')
            };
            const id = payload && payload.id;
            // 获取车辆列表
            const carList = yield put.resolve({
                type: 'getCarList'
            });
            let carNumber = wx.getStorageSync('carNumber');
            if(!carNumber && carList.length){
                carNumber = carList[0].carNumber;
                wx.setStorageSync('carNumber', carNumber);
            }
            // 展示详情
            if(id){
                // 查询汽车列表
                detail = yield put.resolve({
                    type : '$request/post',
                    payload : {
                        url : '/maintenance/getMaintenById',
                        data: {id : Number(id)}
                    }
                });
                detail.price = Number(detail.price)/100;
                detail.time = timeFormat(detail.time, 'YYYY-MM-DD');

            }else{
                detail = {
                    ...detail,
                    'carNumber': carNumber,
                    'mileage': '',
                    'oilFilter': 0, // 机油滤芯
                    'airFilter': 0,
                    'airConFilter': 0,
                    'breakOil': 0,
                    'sparkPlug': 0,
                    'speedOil': 0,
                    'engineOil': 0,
                    'battery': 0,
                    'isDelete': 0
                };
            }
            let list = JSON.parse(JSON.stringify(mlist));
            list.forEach(item => item.value = detail[item.type]);
            list.sort((a,b) => b.value > a.value ? 1 :  -1);
            yield put({
                type : 'save',
                payload : {
                    detail,
                    isEdit: !id,
                    list,
                    location: payload.location
                }
            });
        },

        *submit({ payload }, { put, call, select }){
            const {detail} = yield select(s => s.createMaintain);
            const today = new Date(timeFormat(Date.now(), 'YYYY-MM-DD')).getTime();
            detail.mileage = Number(detail.mileage);
            detail.price = parseInt(detail.price*100);
            detail.time = new Date(detail.time).getTime() + Date.now() - today;
            const result = yield put.resolve({
                type : '$request/post',
                payload : {
                    url : '/maintenance/update',
                    data: detail
                }
            });
            if(result){
                yield put.resolve({
                    type : '$msg/showToast',
                    payload: {
                        title: '保存成功'
                    }
                });
                yield put.resolve({
                    type : '$router/back',
                });
            }
        },

        /**
         * 车牌号修改
         * @param {*} param0 
         * @param {*} param1 
         */
        * carNumberChange({ payload }, { put, call, select }){
            const {detail} = yield select(s => s.createMaintain);
            detail.carNumber = payload.carNumber;
            yield put({
                type : 'save',
                payload : {
                    detail,
                    carModal: false
                }
            });
        }
    }
};